package SiTuQingYou.util;

import javax.servlet.http.HttpServletRequest;

public class Pager {

	private int pageSize = 5;//每页条数，默认5条
	private int dataCount; //数据总数
	private int pageNum;//当前是第几页
	
	
	public static Pager init(HttpServletRequest request)
	{
		Pager pager = new Pager();
		
		//获得当前页码pageNum
    	String pageNumStr = request.getParameter("pageNum");
    	int pageNum = 1;
    	try 
    	{
    		pageNum = Integer.parseInt(pageNumStr);
    	}
    	catch(Exception e) {}
    	pager.setPageNum(pageNum);
    	request.setAttribute("pager",pager);
    	
		return pager;
	}
	
	
	public int getPageCount()//获得总页数
	{
		int pageCount = dataCount/pageSize+(dataCount%pageSize==0?0:1);
		return pageCount;
	}
	
	public int getFirstRow()//获得查数据库的时候，limit后面第一个数是几
	{
		return (pageNum-1)*pageSize;
	}
	
	
	
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getDataCount() {
		return dataCount;
	}
	public void setDataCount(int dataCount) {
		this.dataCount = dataCount;
	}
	public int getPageNum() {
		return pageNum;
	}
	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}
}
